﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BLL;
using VOL;

namespace Shopper
{
    public partial class frmHangHoaManager : DevExpress.XtraEditors.XtraForm
    {
        private DMHangBL hangBL = new DMHangBL();
        public frmHangHoaManager()
        {
            InitializeComponent();
        }

        private void frmHangHoaManager_Load(object sender, EventArgs e)
        {
            LoaiHangBL loaiHangBl = new LoaiHangBL();
            cbLoaiHangHoa.DataSource = loaiHangBl.GetData();
            cbLoaiHangHoa.DisplayMember = "TenLoaiHang";
            cbLoaiHangHoa.ValueMember = "MaLoaiHang";

            NhaCungCapBL nccBL = new NhaCungCapBL();
            DataTable dtNhaCungCap = nccBL.GetData();
            cbNhaCungCap.DataSource = dtNhaCungCap;
            cbNhaCungCap.DisplayMember = "TenNhaCungCap";
            cbNhaCungCap.ValueMember = "MaNhaCungCap";

            FillData();
        }

        private void FillData()
        {
            DataTable dt = hangBL.GetData();
            gridControl1.DataSource = dt;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            using (frmHangHoaForm frm = new frmHangHoaForm())
            {
                if (frm.ShowDialog(this.FindForm()) == System.Windows.Forms.DialogResult.OK)
                {
                    DMHang objDMHang = new DMHang();
                    objDMHang.TenHang = frm.TenHang;
                    objDMHang.DVT = frm.Dvt;
                    objDMHang.DonGia = float.Parse(frm.DonGia);
                    objDMHang.MoTa = frm.MoTa;
                    objDMHang.MaNhaCungCap = frm.MaNhaCungCap;
                    objDMHang.MaLoaiHang = frm.MaLoaiHang;
                    objDMHang.HinhAnh = "";

                    hangBL.insertDMHang(objDMHang);

                    XtraMessageBox.Show("Đã thêm Danh mục hàng hóa thành công !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    FillData();
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {

            using (frmHangHoaForm frm = new frmHangHoaForm())
            {
                int MaHang = (int)gridView1.GetFocusedRowCellValue("MaHang");
                DMHang objDMHangOld = new DMHang();
                objDMHangOld.MaHang = MaHang;

                DataTable dtMDHang = hangBL.searchIDDMHang(objDMHangOld);
                if (dtMDHang.Rows.Count > 0)
                {
                    frm.TenHang = dtMDHang.Rows[0]["TenHang"].ToString();
                    frm.Dvt = dtMDHang.Rows[0]["Dvt"].ToString();
                    frm.DonGia = dtMDHang.Rows[0]["DonGia"].ToString();
                    frm.MoTa = dtMDHang.Rows[0]["MoTa"].ToString();
                    frm.MaNhaCungCap =  (int)dtMDHang.Rows[0]["MaNhaCungCap"];
                    frm.MaLoaiHang = (int)dtMDHang.Rows[0]["MaLoaiHang"];
                }

                if (frm.ShowDialog(this.FindForm()) == System.Windows.Forms.DialogResult.OK)
                {
                    DMHang objDMHang = new DMHang();
                    objDMHang.MaHang = MaHang;
                    objDMHang.TenHang = frm.TenHang;
                    objDMHang.DVT = frm.Dvt;
                    objDMHang.DonGia = float.Parse(frm.DonGia);
                    objDMHang.MoTa = frm.MoTa;
                    objDMHang.MaNhaCungCap = frm.MaNhaCungCap;
                    objDMHang.MaLoaiHang = frm.MaLoaiHang;
                    objDMHang.HinhAnh = "";

                    hangBL.updateDMHang(objDMHang);

                    XtraMessageBox.Show("Đã thay đổi Danh mục hàng hóa thành công !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    FillData();
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult dr = XtraMessageBox.Show("Bạn muốn xóa mặt hàng này không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == System.Windows.Forms.DialogResult.Yes)
            {
                int MaHang = (int)gridView1.GetFocusedRowCellValue("MaHang");
                DMHang objDMHang = new DMHang();
                objDMHang.MaHang = MaHang;

                hangBL.deleteDMHang(objDMHang);
                FillData();
            }
        }
    }
}